﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text.RegularExpressions;
using ZFine.Code;
using ZFine.Data;
using ZFine.Domain.Entity;
using ZFine.Domain.IRepository.SystemManage;
using ZFine.Repository.SystemManage;

namespace Logic.Repository.Project
{
    public class MemberRepository : RepositoryBase<MemberEntity>, IMemberRepository
    {
        public int InsertEnt(MemberEntity entitys)
        {
           //var thisobj= dbcontext.Set<MemberEntity>().Include(f => f.MerchantP).First();

           // dbcontext.Entry<MemberEntity>(entitys).State = EntityState.Added;
            //member.MerchantP = new UserEntity() { F_Id = "9f2ec079-7d0f-4fe2-90ab-8b09a8302aba" };
            //dbcontext.Set<MemberEntity>().fi

            //var userent = new UserEntity() { F_Id = "9f2ec079-7d0f-4fe2-90ab-8b09a8302aba" };
            //var member = dbcontext.Set<MemberEntity>().First();
            //member.MerchantP = userent;
            //var userent = userntrep.FindEntity(f => f.F_Id == "9f2ec079-7d0f-4fe2-90ab-8b09a8302aba");
            dbcontext.Set<MemberEntity>().Attach(entitys).MerchantP = entitys.MerchantP;
            dbcontext.Entry<MemberEntity>(entitys).State = EntityState.Added;
            return dbcontext.SaveChanges();
            //return dbcontext.SaveChanges();
        }
        

        public MemberEntity FindEntitys()
        {
            return dbcontext.Set<MemberEntity>().Include(member => member.MerchantP).FirstOrDefault();
        }

        public int Update(MemberEntity entity)
        {
            dbcontext.Set<MemberEntity>().Attach(entity);
            PropertyInfo[] props = entity.GetType().GetProperties();
            foreach (PropertyInfo prop in props)
            {
                if (prop.GetValue(entity, null) != null)
                {
                    if (prop.PropertyType.FullName.Contains("Entity"))
                    {
                        break;
                    }
                    if (prop.GetValue(entity, null).ToString() == "&nbsp;")
                        dbcontext.Entry(entity).Property(prop.Name).CurrentValue = null;
                    dbcontext.Entry(entity).Property(prop.Name).IsModified = true;
                }
            }
            dbcontext.Entry<MemberEntity>(entity).Reference(f => f.MerchantP).Load();
            dbcontext.Set<MemberEntity>().Attach(entity).MerchantP = entity.MerchantP;
            return dbcontext.SaveChanges();
        }

        public List<MemberEntity> FindList(Pagination pagination)
        {
            bool isAsc = pagination.sord.ToLower() == "asc" ? true : false;
            string[] _order = pagination.sidx.Split(',');
            MethodCallExpression resultExp = null;
            var tempData = dbcontext.Set<MemberEntity>().Include(f=>f.MerchantP).AsQueryable();
            foreach (string item in _order)
            {
                string _orderPart = item;
                _orderPart = Regex.Replace(_orderPart, @"\s+", " ");
                string[] _orderArry = _orderPart.Split(' ');
                string _orderField = _orderArry[0];
                bool sort = isAsc;
                if (_orderArry.Length == 2)
                {
                    isAsc = _orderArry[1].ToUpper() == "ASC" ? true : false;
                }
                var parameter = Expression.Parameter(typeof(MemberEntity), "t");
                var property = typeof(MemberEntity).GetProperty(_orderField);
                var propertyAccess = Expression.MakeMemberAccess(parameter, property);
                var orderByExp = Expression.Lambda(propertyAccess, parameter);
                resultExp = Expression.Call(typeof(Queryable), isAsc ? "OrderBy" : "OrderByDescending", new Type[] { typeof(MemberEntity), property.PropertyType }, tempData.Expression, Expression.Quote(orderByExp));
            }
            tempData = tempData.Provider.CreateQuery<MemberEntity>(resultExp);
            pagination.records = tempData.Count();
            tempData = tempData.Skip<MemberEntity>(pagination.rows * (pagination.page - 1)).Take<MemberEntity>(pagination.rows).AsQueryable();
            return tempData.ToList();
        }

        public List<MemberEntity> FindList(Expression<Func<MemberEntity, bool>> predicate, Pagination pagination)
        {
            bool isAsc = pagination.sord.ToLower() == "asc" ? true : false;
            string[] _order = pagination.sidx.Split(',');
            MethodCallExpression resultExp = null;
            var tempData = dbcontext.Set<MemberEntity>().Include(f=>f.MerchantP).Where(predicate);
            foreach (string item in _order)
            {
                string _orderPart = item;
                _orderPart = Regex.Replace(_orderPart, @"\s+", " ");
                string[] _orderArry = _orderPart.Split(' ');
                string _orderField = _orderArry[0];
                bool sort = isAsc;
                if (_orderArry.Length == 2)
                {
                    isAsc = _orderArry[1].ToUpper() == "ASC" ? true : false;
                }
                var parameter = Expression.Parameter(typeof(MemberEntity), "t");
                var property = typeof(MemberEntity).GetProperty(_orderField);
                var propertyAccess = Expression.MakeMemberAccess(parameter, property);
                var orderByExp = Expression.Lambda(propertyAccess, parameter);
                resultExp = Expression.Call(typeof(Queryable), isAsc ? "OrderBy" : "OrderByDescending", new Type[] { typeof(MemberEntity), property.PropertyType }, tempData.Expression, Expression.Quote(orderByExp));
            }
            tempData = tempData.Provider.CreateQuery<MemberEntity>(resultExp);
            pagination.records = tempData.Count();
            tempData = tempData.Skip<MemberEntity>(pagination.rows * (pagination.page - 1)).Take<MemberEntity>(pagination.rows).AsQueryable();
            return tempData.ToList();
        }
    }
}
